﻿/**
 * Look for suitable index.html file in the webFolder, depending on the userAgent. 
 * remove all not js valid digits from the giver string
 * @param {Object} request The request object
 * @param {Object} response The response object parameter 
 **/
function dispatchHandler ( request, response ) {
	
	var indexPath       = application.webAppService.directoryIndex,
		host 			= request.headers.HOST,
        userAgent		= request.headers.USER_AGENT,
    	conf 			= {},
    	directoryName;
    	
    conf.smartphone 	= "smartphone";
    conf.tablet 		= "tablet";
    conf.desktop 		= "webfolder";
	//look for a suitable directory role depending on the platform
	switch ( true ) {
	
	case (/iPhone|iPod/i).test(userAgent) :
	    
	    directoryName = lookForSmartphonePage( conf);
	     
	break;
	case (/Android/i).test(userAgent) :
		
		if( (/Xoom|Streak|SCH-I800/i).test(userAgent) ) {   //Android tablet divices
		
		    directoryName = lookForTabletPage( conf ); 
		    
		} else {
			
		    if ( (/Mobile/i).test(userAgent) ) {            //Most of Android phone divices have the "Mobile" tag despite of the tablets
		    
    			directoryName = lookForSmartphonePage( conf ); 
    			
    		} else {
    			
    	        directoryName = lookForTabletPage( conf );
    	        
    	    }
		}
	
	break;
	case (/iPad/i).test(userAgent) : 
	    
	    directoryName = lookForTabletPage( conf);
	
	break;
	default: 
	    directoryName = conf.desktop;
	}
	
	//send header location
	if ( directoryName != conf.desktop ) { 
	
	    response.headers.location = "http://" + host + "/" + directoryName + "/" + indexPath;
	    
	} else {
		
		response.headers.location = "http://" + host + "/" + indexPath;
		
	}
	
	return;
}




/**
 * For Tablets, look fisrt for tablet files, then for smartphone or desktop.
 * @param {Object} conf
 **/
function lookForTabletPage( conf ) {
	
	var directoryName;
	
	if( getItemsWithRole(conf.tablet) ) {
	        
		directoryName = conf.tablet;
	    
	} else if (getItemsWithRole(conf.smartphone)){
	    	
	   	directoryName = conf.smartphone;
	    	
	} else {
	    	
	   	directoryName = conf.desktop;
	    
	}
	    
	return directoryName;
	
}

/**
 * For smartphone, look first for smartphone files. Send desktop header if no suitable page is found.
 * @param {Object} conf
 **/
function lookForSmartphonePage( conf ) {
	
	var directoryName;
	
	if( getItemsWithRole(conf.smartphone) ) {
	        
		directoryName = conf.smartphone;
	    
	} else {
	    	
	  	directoryName = conf.desktop;
	    
	}
	    
	return directoryName;
	
}




/*
MOBILE DIVICES USER AGENT :

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

Mozilla/5.0 (Linux; U; Android 0.5; en-us) AppleWebKit/522+ (KHTML, like Gecko) Safari/419.3

Google Nexus

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17


Dell

Mozilla/5.0 (Linux; U; Android 1.6; en-gb; Dell Streak Build/Donut AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/ 525.20.1


HTC


Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; ADR6300 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Mozilla/5.0 (Linux; U; Android 1.6; en-us; WOWMobile myTouch 3G Build/unknown) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1

Mozilla/5.0 (Linux; U; Android 2.2; nl-nl; Desire_A8181 Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

HTC_Dream Mozilla/5.0 (Linux; U; Android 1.5; en-ca; Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1


Motorola

Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Droid Build/FRG22D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.2; en-us; DROID2 GLOBAL Build/S273) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; DROIDX Build/VZW) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 480X854 motorola DROIDX

Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; Droid Build/ESE81) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/ 530.17

Samsung

Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.2; en-ca; SGH-T959D Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Mozilla/5.0 (Linux; U; Android 2.0.1; en-us; Droid Build/ESD56) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17

Sony

Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; E10i Build/2.0.2.A.0.24) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17


*/